package com.td.module.farmer.service.management;

import com.td.framework.common.pojo.PageResult;
import com.td.module.farmer.controller.admin.management.vo.*;
import com.td.module.farmer.controller.admin.plantinginformation.vo.PlantingInformationCreateReqVO;
import com.td.module.farmer.controller.admin.plantinginformation.vo.PlantingInformationRespVO;
import com.td.module.farmer.controller.admin.plantinginformation.vo.PlantingInformationUpdateReqVO;
import com.td.module.farmer.convert.management.ManagementConvert;
import com.td.module.farmer.convert.plantinginformation.PlantingInformationConvert;
import com.td.module.farmer.dal.dataobject.management.ManagementDO;
import com.td.module.farmer.dal.dataobject.plantinginformation.PlantingInformationDO;
import com.td.module.farmer.dal.mysql.management.ManagementMapper;
import com.td.module.farmer.dal.mysql.plantinginformation.PlantingInformationMapper;
import com.td.module.farmer.service.plantinginformation.PlantingInformationService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.td.module.farmer.enums.ErrorCodeConstants.MANAGEMENT_NOT_EXISTS;
import static com.td.module.farmer.enums.ErrorCodeConstants.PLANTING_INFORMATION_NOT_EXISTS;

/**
 * 农户土地信息 Service 实现类
 *
 * @author 王翔翔
 */
@Service
@Validated
public class ManagementServiceImpl implements ManagementService {

    @Resource
    private ManagementMapper managementMapper;

    @Resource
    private PlantingInformationMapper plantingInformationMapper;

    @Resource
    private PlantingInformationService plantingInformationService;

    private BigDecimal createPlating(List<PlantingInformationCreateReqVO> plantingInformationList, Long id) {

        BigDecimal landAreas = BigDecimal.valueOf(0);
        for (PlantingInformationCreateReqVO plantingInformationCreateReqVO : plantingInformationList) {
            PlantingInformationDO plantingInformationDO = PlantingInformationConvert.INSTANCE.convert(plantingInformationCreateReqVO);
            plantingInformationDO.setFarmerId(id);
            plantingInformationMapper.insert(plantingInformationDO);
            //计算总面积
            landAreas = plantingInformationDO.getLandArea().add(landAreas);
        }
        return landAreas;
    }

    private BigDecimal updatePlating(List<PlantingInformationUpdateReqVO> plantingInformationList, Long id) {

        BigDecimal landAreas = BigDecimal.valueOf(0);
        for (PlantingInformationUpdateReqVO plantingInformationUpdateReqVO : plantingInformationList) {
            PlantingInformationDO plantingInformationDO = PlantingInformationConvert.INSTANCE.convert(plantingInformationUpdateReqVO);
            if(plantingInformationDO.getFarmerId() != null && plantingInformationDO.getId() != null){
                plantingInformationMapper.updateById(plantingInformationDO);
            }else {
                plantingInformationDO.setFarmerId(id);
                plantingInformationMapper.insert(plantingInformationDO);
            }
            //计算总面积
            landAreas = plantingInformationDO.getLandArea().add(landAreas);
        }
        return landAreas;
    }

    @Override
    public Long createManagement(ManagementCreateReqVO createReqVO) {
        ManagementDO management = ManagementConvert.INSTANCE.convert(createReqVO);
        managementMapper.insert(management);
        // 判断地块列表,并计算地块总面积
        if (createReqVO.getPlantingInformationList().size() != 0) {
            BigDecimal landAreas = createPlating(createReqVO.getPlantingInformationList(), management.getId());
            management.setLandArea(landAreas);
            int count = Math.toIntExact(plantingInformationMapper.selectCount(management.getId()));
            management.setLandNum(count);
        }else{
            throw exception(PLANTING_INFORMATION_NOT_EXISTS);
        }
        management.setFarmerCode(management.getId().toString());
        managementMapper.updateById(management);
        // 返回
        return management.getId();
    }

    @Override
    public void updateManagement(ManagementUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateManagementExists(updateReqVO.getId());
        // 更新
        ManagementDO updateObj = ManagementConvert.INSTANCE.convert(updateReqVO);
        updateObj.setLandNum(Math.toIntExact(plantingInformationMapper.selectCount(updateObj.getId())));
        updateObj.setLandArea(updatePlating(updateReqVO.getPlantingInformationList(), updateObj.getId()));
        if(updateReqVO.getDeleteIds().size() > 0){
            plantingInformationMapper.deleteBatchIds(updateReqVO.getDeleteIds());
        }
        managementMapper.updateById(updateObj);
    }

    @Override
    public void deleteManagement(Long id) {
        // 校验存在
        this.validateManagementExists(id);
        // 删除
        plantingInformationMapper.delete(id);
        managementMapper.deleteById(id);
    }

    private void validateManagementExists(Long id) {
        if (managementMapper.selectById(id) == null) {
            throw exception(MANAGEMENT_NOT_EXISTS);
        }
    }

    @Override
    public ManagementRespVO getManagement(Long id) {
        //农户信息
        List<PlantingInformationDO> plantingInformationDOList = plantingInformationService.getAllPlanting(id);
        List<PlantingInformationRespVO> plantingInformationRespVOList = PlantingInformationConvert.INSTANCE.convertList(plantingInformationDOList);

        ManagementRespVO managementRespVO = ManagementConvert.INSTANCE.convert(managementMapper.selectById(id));
        managementRespVO.setPlantingInformationList(plantingInformationRespVOList);

        return managementRespVO;
    }

    @Override
    public ManagementDO getManagement(String farmerCode) {
        return managementMapper.selectByFarmerCode(farmerCode);
    }

    @Override
    public List<ManagementDO> getManagementList(Collection<Long> ids) {
        return managementMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ManagementDO> getManagementPage(ManagementPageReqVO pageReqVO) {
        return managementMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ManagementDO> getManagementList(ManagementExportReqVO exportReqVO) {
        return managementMapper.selectList(exportReqVO);
    }

    @Override
    public ManagementRespVO getMobileManagement(String mobile) {

        ManagementDO managementDO = managementMapper.selectByMobile(mobile);
        if (managementDO == null) {
            return new ManagementRespVO();
        }
        List<PlantingInformationDO> plantingInformationDOList = plantingInformationMapper.selectListPlating(managementDO.getId());
        List<PlantingInformationRespVO> plantingInformationRespVOList = PlantingInformationConvert.INSTANCE.convertList(plantingInformationDOList);

        ManagementRespVO managementRespVO = ManagementConvert.INSTANCE.convert(managementDO);
        managementRespVO.setPlantingInformationList(plantingInformationRespVOList);
        return managementRespVO;
    }


}
